package com.duanjw.springcloud.openfeign.consumer.web;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.duanjw.springcloud.openfeign.consumer.feign.NacosConfigClient;
import com.duanjw.springcloud.openfeign.consumer.feign.ProviderClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 消费者 controller
 *
 * @author duanjw
 */
@RestController
@RequestMapping("consumer")
public class ConsumerController {

    /**
     * 向注入普通 bean 一样注入服务提供者接口
     */
    private final ProviderClient providerClient;
    private final NacosConfigClient nacosConfigClient;
    /**
     * 用户获取当前服务信息
     */
    private NacosDiscoveryProperties nacosDiscoveryProperties;

    public ConsumerController(NacosDiscoveryProperties nacosDiscoveryProperties, ProviderClient providerClient, NacosConfigClient nacosConfigClient) {
        this.nacosDiscoveryProperties = nacosDiscoveryProperties;
        this.providerClient = providerClient;
        this.nacosConfigClient = nacosConfigClient;
    }

    /**
     * echo
     * <p>
     * 访问地址：curl localhost:7007/consumer/echo/OpenFeignProvider
     *
     * @param value
     * @return
     */
    @GetMapping("echo/{value}")
    public String hello(@PathVariable String value) {
        String ip = nacosDiscoveryProperties.getIp();
        int port = nacosDiscoveryProperties.getPort();
        return String.format("Hello %s，openfeign-provider ip: %s，port： %s", value, ip, port);
    }

    /**
     * 使用 openfeign 调用 openfeign 服务
     * <p>
     * 请求地址：curl localhost:7007/consumer/openfeign
     *
     * @return
     */
    @GetMapping("openfeign")
    public String openfeign() {
        String value = "Openfeign";
        return providerClient.hello(value, "张");
    }

    /**
     * 使用 openfeign 调用 config 服务
     * <p>
     * 请求地址：curl localhost:7007/consumer/config
     *
     * @return
     */
    @GetMapping("config")
    public String config() {
        String value = "name";
        return nacosConfigClient.config(value);
    }
}